//
//  ActivityViewController.m
//  豆瓣2
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 张鸿峰. All rights reserved.
//

#import "ActivityViewController.h"

#import "ActivityListTableViewCell.h"

#import "AppTools.h"

#import "ActivityList.h"

#import "ActivityDetailViewController.h"

#import "DataBaseSingleTon.h"

#import "MJRefresh.h"  //刷新


//@interface ActivityViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate>
@interface ActivityViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>


@property (nonatomic,retain) UITableView *myTableView;
@property (nonatomic,retain) NSMutableArray *allArray;
@property (nonatomic,retain) NSMutableArray *contentArray;

//@property (nonatomic,retain) UISearchController *searchController;
@property (nonatomic,retain) NSMutableArray *searchArray;
@property(nonatomic,retain)UISearchBar *mySearchBar;
@property(nonatomic,retain)UISearchDisplayController *searchDC;


@end

@implementation ActivityViewController


- (void)dealloc
{
    [_myTableView release];
    [_allArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//     Do any additional setup after loading the view.
    
    self.navigationItem.title = @"活动";
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [self.myTableView release];
    
    [self.myTableView registerClass:[ActivityListTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    self.searchArray = [NSMutableArray array];
    
    //搜索
    
    /*
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    //    设置开始搜索时背景显示与否 页面背景阴影效果,一般不要设置.
    self.searchController.dimsBackgroundDuringPresentation = NO;
    //设置点击搜索框时候隐藏导航栏
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    //    [searchBar sizeToFit]:设置searchBar位置自适应
    [self.searchController.searchBar sizeToFit];
    
    self.myTableView.tableHeaderView = self.searchController.searchBar;  // ***
    */
    
    
    self.mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    self.mySearchBar.delegate = self;
    [self.mySearchBar setPlaceholder:@"搜索列表"];
    /*
     UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类。
     里面内置了一个 UITableView 用于显示搜索的结果。它可以和一个需要搜索功能的
     controller关联起来
     */
    self.searchDC = [[UISearchDisplayController alloc]initWithSearchBar:self.mySearchBar contentsController:self];
    self.searchDC.active = NO;
    self.searchDC.searchResultsDataSource = self;
    self.searchDC.searchResultsDelegate = self;
    
    self.myTableView.tableHeaderView = self.mySearchBar; // ***

    
    
    
    //缓存
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *activityPath = [document stringByAppendingPathComponent:@"activity.plist"];
    
    //反归档 取
    NSMutableArray *array = [NSMutableArray array];
    array = [NSKeyedUnarchiver unarchiveObjectWithFile:activityPath];
    self.allArray = [NSMutableArray arrayWithArray:array];
    
    [self.myTableView reloadData];
    
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    
    [AppTools getLocalDataWithURL:urlStr Block:^(NSMutableDictionary *dic) {
        
        NSMutableArray *array = [dic objectForKey:@"events"];
        
        self.allArray = [NSMutableArray array];
        self.contentArray = [NSMutableArray array];

    
        for (NSMutableDictionary *dic in array) {
            
            ActivityList *activityList = [[ActivityList alloc] init];
            [activityList setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:activityList];
            [activityList release];
            
            ActivityDetail *activityDetail = [[ActivityDetail alloc] init];
            activityDetail.content = [dic objectForKey:@"content"];
            [self.contentArray addObject:activityDetail];
            [activityDetail release];
            
            
            
            //缓存
            //归档  存
            NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *activityPath = [document stringByAppendingPathComponent:@"activity.plist"];
            [NSKeyedArchiver archiveRootObject:self.allArray toFile:activityPath];
            
            
        }
        
        [self.myTableView reloadData];
    }];

    [[DataBaseSingleTon shareDataBase] openDBAC];
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return self.allArray.count;
    
//    if (self.searchController.active) {
//        return self.searchArray.count;
//    }
//    else {
//        return self.allArray.count;
//    }
    
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return self.searchArray.count;
    }
    else {
        return self.allArray.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    ActivityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
//    cell.textLabel.text = @"中国人";
    
//    ActivityList *activityList = [self.allArray objectAtIndex:indexPath.row];
//    cell.myActivityList = activityList;
    
    if (cell == nil) {
        
        cell = [[ActivityListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        ActivityList *activityList = [self.searchArray objectAtIndex:indexPath.row];
        cell.myActivityList = activityList;
    }
    else {
        ActivityList *activityList = [self.allArray objectAtIndex:indexPath.row];
        cell.myActivityList = activityList;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    ActivityList *activityList = [self.allArray objectAtIndex:indexPath.row];
    
    ActivityDetail *activityDetail = [self.contentArray objectAtIndex:indexPath.row];
    
    ActivityDetailViewController *activityDetailVC = [[ActivityDetailViewController alloc] init];
    activityDetailVC.ADIndexPath = indexPath;
    activityDetailVC.activityList = activityList;
    
    activityDetailVC.activityDetail = activityDetail;
    
    [self.navigationController pushViewController:activityDetailVC animated:YES];
    [activityDetailVC release];
    */
    
    
    ActivityList *activityList = nil;
    
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        
        NSLog(@"选中搜索列表");
        activityList = [self.searchArray objectAtIndex:indexPath.row];
        
    }
    else {
        
        NSLog(@"选中所有列表");
        activityList = [self.allArray objectAtIndex:indexPath.row];
        
    }
    
    ActivityDetail *activityDetail = [self.contentArray objectAtIndex:indexPath.row];
    ActivityDetailViewController *activityDetailVC = [[ActivityDetailViewController alloc] init];
    activityDetailVC.ADIndexPath = indexPath;
    activityDetailVC.activityList = activityList;
    
    activityDetailVC.activityDetail = activityDetail;
    
    [self.navigationController pushViewController:activityDetailVC animated:YES];
    [activityDetailVC release];
    
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 185;
}

//-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
//输入文字就触发的方法
//    NSString *searchString = [self.searchController.searchBar text];

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    /*
    NSLog(@"===%@",searchString);
    
    [self.searchArray removeAllObjects];
    for (ActivityList *activityList in self.allArray) {
        
        //rangeOfString:方法是在movieName中搜索是否包含searchText
        //rangeOfString 前面的参数是要被搜索的字符串，后面的是要搜索的字符
        //NSNotFound 表示请求操作的某个内容没有发现
        if ([activityList.title rangeOfString:searchString].location != NSNotFound) {
            
            [self.searchArray addObject:activityList];
        }
    }
    NSLog(@"searchArray===%@",self.searchArray);
    
    [self.myTableView reloadData];
    */
    
    NSLog(@"输入框中的内容=====%@",searchText);
    [self.searchArray removeAllObjects];
    for (ActivityList *activityList in self.allArray) {
        
        //rangeOfString:方法是在movieName中搜索是否包含searchText
        //rangeOfString 前面的参数是要被搜索的字符串，后面的是要搜索的字符
        //NSNotFound 表示请求操作的某个内容没有发现
        if ([activityList.title rangeOfString:searchText].location != NSNotFound) {
            
            [self.searchArray addObject:activityList];
        }
    }
    
}



/*
//UISearchController的移除
//在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在.
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.searchController.active) {
        self.searchController.active = NO;
        [self.searchController.searchBar removeFromSuperview];
    }
}
*/



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
